package com.cxx.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxx.admin.service.BlogManageService;
import com.cxx.common.ReadView;
import com.cxx.common.WriteView;
import com.cxx.admin.dto.blog.BlogCategoryDto;
import com.cxx.admin.dto.blog.BlogDto;
import com.cxx.admin.dto.blog.BlogVisitDto;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/blog-manage")
@Tag(name = "博客管理")
public class BlogManageController {
    @Resource
    private BlogManageService blogManageService;

    @Operation(summary = "分页查询博客")
    @GetMapping("/page")
    public IPage<BlogDto> queryBlogByPage(@RequestParam("currentPage") Integer currentPage,
                                          @RequestParam("pageSize") Integer pageSize) {
        return blogManageService.queryBlogByPage(currentPage, pageSize);
    }

    @Operation(summary = "查询博客内容")
    @GetMapping("/content/{id}")
    public @JsonView(ReadView.class) BlogDto queryBlogById(@PathVariable("id") long id) {
        return blogManageService.queryBlogById(id);
    }

    @Operation(summary = "查询博客分类")
    @GetMapping("/category")
    public List<BlogCategoryDto> queryBlogCategory() {
        return blogManageService.queryBlogCategory();
    }

    @Operation(summary = "增加博客")
    @PostMapping("")
    public Boolean addBlog(@JsonView(WriteView.class) @RequestBody BlogDto blog) {
        return blogManageService.addBlog(blog);
    }

    @Operation(summary = "更新博客")
    @PutMapping("/{id}")
    public Boolean updateBlog(@PathVariable("id") long id,
                              @JsonView(WriteView.class) @RequestBody BlogDto blog) {
        return blogManageService.updateBlog(id, blog);
    }

    @Operation(summary = "删除博客")
    @DeleteMapping("/{id}")
    public Boolean deleteBlog(@PathVariable("id") long id) {
        return blogManageService.deleteBlog(id);
    }

    @Operation(summary = "查询博客访问")
    @GetMapping("/visit")
    public IPage<BlogVisitDto> queryBlogVisit(@RequestParam("currentPage") Integer currentPage,
                                              @RequestParam("pageSize") Integer pageSize) {
        return blogManageService.queryBlogVisit(currentPage, pageSize);
    }
}
